<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="../ElementUI/index.css">

</head>
<body>

<div id="app">
    <el-container>
        <el-aside width="250px">
            <el-tree
                    :data="data"
                    node-key="id"
                    default-expand-all
                    :expand-on-click-node="false">
      <span class="custom-tree-node" slot-scope="{ node, data }">
        <span>{{ data.name }}</span>
          <span>
          <i class="el-icon-circle-plus-outline" @click="add(data)"></i>
        </span>
          <span>
          <i class="el-icon-edit" @click="edit(node,data)"></i>
        </span>
          <span>
          <i class="el-icon-remove-outline" @click="remove(node,data)"></i>
        </span>
      </span>
            </el-tree>
        </el-aside>
        <el-main>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" label="序号" align="center"></el-table-column>
                <el-table-column prop="name" label="菜单名称" align="center"></el-table-column>
                <el-table-column prop="linkUrl" label="跳转页面" align="center"></el-table-column>
                <el-table-column prop="path" label="路径" align="center"></el-table-column>
                <el-table-column prop="priority" label="优先级" align="center"></el-table-column>
                <el-table-column prop="icon" label="图标" align="center"></el-table-column>
                <el-table-column prop="description" label="菜单说明" align="center"></el-table-column>
                <el-table-column prop="parentMenuId" label="父级菜单id" align="center"></el-table-column>
                <el-table-column prop="level" label="级别" align="center"></el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagination"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
        </el-main>
    </el-container>





    <el-dialog title="新增菜单" :visible.sync="dialogFormVisible">
        <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
            <el-form-item label="菜单名称" prop="name">
                <el-input v-model="formData.name"></el-input>
            </el-form-item>
            <el-form-item label="联系地址">
                <el-input v-model="formData.linkUrl"></el-input>
            </el-form-item>
            <el-form-item label="菜单说明">
                <el-input v-model="formData.description"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="doAdd()">确 定</el-button>
        </div>
    </el-dialog>

    <el-dialog title="编辑菜单" :visible.sync="dialogFormVisible4Edit">
        <el-form ref="dataAddForm4" :model="formData4" :rules="rules" label-position="right" label-width="100px">
            <el-form-item label="菜单名称" prop="name">
                <el-input v-model="formData4.name"></el-input>
            </el-form-item>
            <el-form-item label="联系地址">
                <el-input v-model="formData4.linkUrl"></el-input>
            </el-form-item>
            <el-form-item label="菜单说明">
                <el-input v-model="formData4.description"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible4Edit = false">取 消</el-button>
            <el-button type="primary" @click="editMenu()">确 定</el-button>
        </div>
    </el-dialog>
</div>

<script src="../ElementUI/vue.js"></script>
<script src="../ElementUI/index.js"></script>
<script src="../js/axios-0.18.0.js"></script>

<script>
    var vue = new Vue({
        el: "#app",
        data: {
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize:10,//每页显示的记录数
                total:0,//总记录数
                queryString:null//查询条件
            },
            data: [
                {label:''}
            ],
            formData:{},
            formData4:{},
            dataList:[],
            dialogFormVisible:false,
            dialogFormVisible4Edit:false,
            rules: {//校验规则
                name: [{ required: true, message: '菜单名称为必填项', trigger: 'blur' }]
            }
        },
        created() {
            //钩子函数
            this.findPage();
            this.findRightPage();
        },
        methods: {
            //切换页码
            handleCurrentChange(currentPage) {
                console.log(currentPage);
                /*console.log(this.pagination.currentPage);*/
                this.pagination.currentPage = currentPage;
                this.findRightPage();

            },
            findRightPage() {
                //提交当前页面 ，每页显示条数 ，查询条件
                axios.post("/user/findRightPage.do",this.pagination).then((res)=>{
                    //console.log(res);
                    // res.data = {total: 68, rows: Array(10)}
                    //总条数给分页插件
                    this.pagination.total = res.data.total;
                    //查询的数据给表格
                    this.dataList = res.data.rows;
                })
            },

            findPage(){
                axios.get("/user/findAllMenu.do",).then((res) => {
                    console.log(res);
                    this.data = res.data.data;
                });
            },
            //给添加按钮绑定添加事件
            add(data){
                this.formData = {};

                 this.formData.parentMenuId = data.id;
                //弹出新增框
                this.dialogFormVisible = true;
            },
            remove(data){
                //console.log(data.data.id);
                this.$confirm('是否删除', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios.delete("/user/remove.do",{params:{
                            id:data.data.id
                        }}).then( (res) => {
                        //console.log(res);
                        //刷新页面
                        this.findPage();
                        //提示用户
                        this.$message({
                            message: res.data.message,
                            type: res.data.flag ? 'success' : "error" //success/warning/info/error
                        });

                    }).catch((error)=>{

                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });

            },
            doAdd() {
                //表单验证
                this.$refs.dataAddForm.validate((flag) => {
                    if (flag) {
                        //提交数据
                        // console.log(this.formData);
                        axios.post("/user/doAdd.do",this.formData).then((res) => {
                            if(res.data.flag){
                                //关闭弹框
                                this.dialogFormVisible = false;
                                //刷新页面
                                this.findPage();
                            }
                            //不管成功失败都要提示用户
                            this.$message({
                                message: res.data.message,
                                type: res.data.flag ? 'success' : "error" //success/warning/info/error
                            });
                        }).catch((error) =>{
                            console.log(error);
                        })
                    } else {
                        //失败给提示
                        this.$message.error('表单校验失败');//提示信息我们叫文案（一般产品提供）
                        return false;//固定写法校验失败返回false
                    }
                });
            },
            edit(node, data) {
                axios.get("/user/findById.do?id=" + data.id).then((res)=>{
                    // res.data = {data:object,flag:boolean,message:string}
                    this.formData4 = res.data.data;
                    //弹出编辑框
                    this.dialogFormVisible4Edit = true;
                })
            },
            editMenu() {
                this.$refs.dataAddForm4.validate((flag) => {
                    if (flag) {
                        axios.put("/user/edit.do", this.formData4).then((res) => {
                            //提示用户
                            this.$message({
                                message: res.data.message,
                                type: res.data.flag ? 'success' : "error" //success/warning/info/error
                            });
                            //关闭弹框
                            this.dialogFormVisible4Edit = false;
                            //刷新页面
                            this.findPage();
                        });
                    } else {
                        //失败给提示
                        this.$message.error('表单校验失败');//提示信息我们叫文案（一般产品提供）
                        return false;//固定写法校验失败返回false
                    }

                })
            }

        }
    })
</script>
</body>
</html>